package com.lac.component.thread;

import com.lac.component.dao.GoodsDao;
import com.lac.component.model.Goods;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author xiaokuixiao
 * date2025/7/7 10:50
 */
public class ThreadPoolConfigExample {

    // 内部类，实现 Callable，用于提交到线程池的任务
    public  static  class GoodsInsertTask implements Callable<Integer>  {
        private final GoodsDao goodsDao;
        private final List<Goods> goodsList;

        public  GoodsInsertTask(GoodsDao goodsDao, List<Goods> goodsList) {
            this.goodsDao = goodsDao;
            this.goodsList = goodsList;
        }

        @Override
        public Integer call() {
            int insertedCount = 0;
                try {
                    int result = goodsDao.insertGoodsList(goodsList);
                    insertedCount += result;
                } catch (Exception e) {
                    // 处理插入异常，例如记录失败的商品信息和异常信息
                    System.err.println("插入商品失败: "  + ", 错误信息: " + e.getMessage());

            }
            return insertedCount;
        }
    }

    public  static  class GoodsInsertTask1 implements Runnable  {
        private final GoodsDao goodsDao;
        private final List<Goods> goodsList;

        public  GoodsInsertTask1(GoodsDao goodsDao, List<Goods> goodsList) {
            this.goodsDao = goodsDao;
            this.goodsList = goodsList;
        }


        @Override
        public void run() {
            int insertedCount = 0;

                try {
                    int result = goodsDao.insertGoodsList(goodsList);
                    insertedCount += result;
                } catch (Exception e) {
                    // 处理插入异常，例如记录失败的商品信息和异常信息
                    System.err.println("插入商品失败: "  + ", 错误信息: " + e.getMessage());
                }
        }
    }

    // 生成测试数据的辅助方法
    public static List<Goods> generateGoodsData(int count) {
        List<Goods> goodsList = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            Goods goods = new Goods();
            goods.setGoodsId("G" + String.format("%03d", i));
            goods.setGoodsName("商品" + i);

            goods.setGoodsPrice(new BigDecimal("10.00"));
            goods.setGoodsCount(i + 1);
            goodsList.add(goods);
        }
        return goodsList;
    }
}